<?php

namespace Biz\model\redPacket;

use Doraemon\pockets\datebase\BaseDbModel;
use Medoo\Medoo;

class RedPackets extends BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_redpacket';

    const _DATABASE_ = 'biz_red_packet';

    const _TABLE_NAME = 'red_packets';

    public static function award($uid, $configId, $amount)
    {
        $data = [
            'uid' => $uid,
            'config_id' => $configId,
            'packet_amount' => $amount,
            'current_amount' => $amount,
            'deduct_amount' => 0,
        ];
        $packetId = self::addData($data);
        return $packetId;
    }

    public static function addData($data)
    {
        $now = date('Y-m-d H:i:s');
        $data['addtime'] = $now;
        $data['edittime'] = $now;
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}

